<template>
  <div class="time-selector">
    <div class="type-check">
      <span class="type-label">时间范围：</span>
      <el-radio-group v-model="timeType" @change="changeType">
        <el-radio :label="'1'">近一天</el-radio>
        <el-radio :label="'2'">近一周</el-radio>
        <el-radio :label="'3'">近一月</el-radio>
        <el-radio :label="'4'">近一年</el-radio>
        <el-radio :label="'0'">自定义</el-radio>
      </el-radio-group>
    </div>
    <div class="time-check" v-if="this.timeType==='0'">
      <el-date-picker
        v-model="timeRange"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        value-format="yyyy-MM-dd"
        class="timeSelect"
        popper-class="timeSelectPopup"
      ></el-date-picker>
    </div>
    <div class="time-btns">
      <div class="reset-btn" @click="resetTime">重置</div>
      <div class="save-btn" @click="saveTime">确定</div>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import moment from 'moment'
export default {
  components: {
  },
  name: "",
  props: {
    nowStartTime: {
      type: String,
      default: '',
    },
    nowEndTime: {
      type: String,
      default: '',
    },
    nowTimeType: {
      type: String | Number,
      default: '0',
    },
  },
  data() {
    return {
      timeRange: ['',''],
      timeType: '0',
    }
  },
  computed: {
  },
  mounted() {
    this.timeType = this.nowTimeType || '0'
    this.timeRange = [this.nowStartTime || '',this.nowEndTime || '']
  },
  methods: {
    resetTime() {
      this.timeRange = ['','']
      this.timeType = '0'
    },
    saveTime() {
      if(this.timeType === '1'){
        this.timeRange = [
          moment().subtract(1,'day').format('YYYY-MM-DD'),
          moment().format('YYYY-MM-DD')
        ]
      }
      else if(this.timeType === '2'){
        this.timeRange = [
          moment().subtract(1,'week').format('YYYY-MM-DD'),
          moment().format('YYYY-MM-DD')
        ]
      }
      else if(this.timeType === '3'){
        this.timeRange = [
          moment().subtract(1,'month').format('YYYY-MM-DD'),
          moment().format('YYYY-MM-DD')
        ]
      }
      else if(this.timeType === '4'){
        this.timeRange = [
          moment().subtract(1,'year').format('YYYY-MM-DD'),
          moment().format('YYYY-MM-DD')
        ]
      }
      this.$emit('save',this.timeType,this.timeRange)
    },
    changeType(){},
  },
};
</script>
<style>
.timeSelectPopup {
  z-index: 100001 !important;
}
</style>
<style lang="less" scoped>
@textColor: #1f1e1d;
@ntalkColor: #e67529;
.time-selector {
  position: absolute;
  top: 26px;
  right: 0;
  width: 450px;
  padding: 16px;
  box-sizing: border-box;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0px 6px 16px 0px rgba(0, 0, 0, 0.08),0px 3px 6px -4px rgba(0, 0, 0, 0.12),0px 9px 28px 8px rgba(0, 0, 0, 0.05);
  z-index: 100000;
  .type-check {
    width: 100%;
    display: flex;
    align-items: center;
    .type-label {
      font-size: 14px;
      color: rgba(0,0,0,0.66);
    }
    /deep/ .el-radio {
      margin-right: 8px;
      &:last-child {
        margin-right: 0;
      }
    }
    /deep/ .el-radio__label {
      padding-left: 6px;
    }
    /deep/ .el-radio__input.is-checked .el-radio__inner {
      border-color: @ntalkColor;
      background: @ntalkColor;
    }
    /deep/ .el-radio__input.is-checked+.el-radio__label {
      color: @ntalkColor;
    }
  }
  .time-check {
    width: 100%;
    height: 32px;
    margin-top: 12px;
    display: flex;
    justify-content: flex-end;
    .timeSelect {
      width: 380px !important;
      height: 32px;
      line-height: 32px;
      /deep/ .el-range__icon {
        margin-top: -8px;
      }
      /deep/ .el-range-separator {
        margin-top: -8px;
      }
      /deep/ .el-range__close-icon {
        margin-top: -8px;
      }
    }
    /deep/ .el-range-editor.is-active, .el-range-editor.is-active:hover, .el-select .el-input.is-focus .el-input__inner {
      border-color: @ntalkColor;
    }
  }
  .time-btns {
    width: 100%;
    height: 32px;
    margin-top: 16px;
    display: flex;
    justify-content: flex-end;
    .reset-btn {
      width: 60px;
      height: 100%;
      box-sizing: border-box;
      font-size: 14px;
      display: flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;
      border-radius: 6px;
      color: @textColor;
      margin-right: 8px;
      border: 1px solid #f2f3f6;
    }
    .save-btn {
      width: 60px;
      height: 100%;
      box-sizing: border-box;
      font-size: 14px;
      display: flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;
      border-radius: 6px;
      background-color: @ntalkColor;
      color: #fff;
    }
  }
}
</style>
